package com.lwj.liwanjia.view.activity.newproduct;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.ProductMoreAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.AddGwcBean;
import com.lwj.liwanjia.bean.MoreBannerBean;
import com.lwj.liwanjia.bean.NewProductMoreBean;
import com.lwj.liwanjia.impl.AddGwcView;
import com.lwj.liwanjia.impl.MoreBannerView;
import com.lwj.liwanjia.impl.NewProductMoreView;
import com.lwj.liwanjia.presenter.AddGwcPresenter;
import com.lwj.liwanjia.presenter.MoreBannerPresenter;
import com.lwj.liwanjia.presenter.NewProductMorePresenter;
import com.lwj.liwanjia.utils.GlideImageLoader;
import com.lwj.liwanjia.utils.NetWorkUtil;
import com.lwj.liwanjia.utils.PopUtils;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.login.LoginActivity;
import com.lwj.liwanjia.view.activity.supermarket.ShangpinDetailActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NewProductMoreActivity extends BaseActivity implements NewProductMoreView, MoreBannerView, SuperRecyclerView.LoadingListener, OnBannerClickListener {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.product_rlv)
    SuperRecyclerView productRlv;
    @BindView(R.id.progress)
    ProgressBar progressBar;

    private SpfUtils spfUtils;
    private String token;
    private List<String> imageList = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private List<NewProductMoreBean.DataBean.CommendityBean> list = new ArrayList<>();
    private ProductMoreAdapter adapter;
    private NewProductMorePresenter morePresenter;
    private MoreBannerPresenter bannerPresenter;
    private int page = 0;
    private int allPage;
    private Banner productBanner;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("新品展示");
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(R.mipmap.fenlei);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        productRlv.setLayoutManager(manager);
        productRlv.setRefreshEnabled(true);
        productRlv.setLoadMoreEnabled(true);
        productRlv.setLoadingListener(this);
        adapter = new ProductMoreAdapter(this, list);
        addView();
        productRlv.setAdapter(adapter);
        morePresenter = new NewProductMorePresenter(this);
        bannerPresenter = new MoreBannerPresenter(this);
        bannerPresenter.getMoreBanner();
        listener();
    }

    private void addView() {
        View view = LayoutInflater.from(this).inflate(R.layout.banner_head, null);
        productBanner = (Banner) view.findViewById(R.id.product_banner);
        adapter.addHeaderView(view);
    }

    private void listener() {
        adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent = new Intent(NewProductMoreActivity.this, ShangpinDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("shopGoodsCode", list.get(position).getCommodityCode());
                bundle.putString("commodityType", list.get(position).getCommodityType());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        morePresenter.getNewProductMore(page);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_new_product_more;
    }

    @OnClick({R.id.tv_left, R.id.iv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.iv_right:
                PopUtils.getInstance().showPopupWindow(NewProductMoreActivity.this, ivRight);
                break;
        }
    }

    @Override
    public void OnBannerClick(int position) {

    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getMoreBanner(MoreBannerBean moreBannerBean) {
        if (moreBannerBean.getState() == 1) {
            imageList.clear();
            titles.clear();
            List<MoreBannerBean.DataBean> data = moreBannerBean.getData();
            for (int i = 0; i < data.size(); i++) {
                imageList.add(data.get(i).getImgUrl());
                titles.add(data.get(i).getCommodityDescribe());
            }
            productBanner.setImages(imageList)
                    .setImageLoader(new GlideImageLoader())
                    .setOnBannerClickListener(this)
                    .setBannerTitles(titles)
                    .setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE)
                    .setDelayTime(4000)
                    .start();
        }
    }

    @Override
    public void getNewProductMore(NewProductMoreBean newProductMoreBean) {
        if (newProductMoreBean.getState() == 1) {
            allPage = newProductMoreBean.getData().get(0).getAllPage();
            if (page == 0) {
                list.clear();
            }
            list.addAll(newProductMoreBean.getData().get(0).getCommendity());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (morePresenter != null) {
            morePresenter = null;
        }
        if (bannerPresenter != null) {
            bannerPresenter = null;
        }
    }

    @Override
    public void onRefresh() {
        page = 0;
        initData();
        productRlv.completeRefresh();
    }

    @Override
    public void onLoadMore() {
        if (page >= allPage - 1) {
            productRlv.setNoMore(true);
            return;
        }
        page++;
        initData();
        productRlv.completeLoadMore();
    }
}
